package csu.jpetstore.jpetstore.service;

import com.sun.istack.NotNull;
import csu.jpetstore.jpetstore.domain.AdminRole;
import csu.jpetstore.jpetstore.domain.AdminUserRole;
import csu.jpetstore.jpetstore.domain.UserAuths;
import csu.jpetstore.jpetstore.domain.Users;
import csu.jpetstore.jpetstore.repository.UserAuthsRepository;
import csu.jpetstore.jpetstore.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserAuthsService {
    @Autowired
    UserAuthsRepository userAuthsRepository;
    @Autowired
    UsersRepository usersRepository;
    @Autowired
    AdminUserRoleService userRoleService;
    @Autowired
    AdminRoleService roleService;
    public boolean isUsernameExist(@NotNull String username){
        return userAuthsRepository.existsByUsername(username);
    }
    @Transactional
    public void add(@NotNull UserAuths userAuths){
            usersRepository.save(userAuths.getUser());
            userAuthsRepository.save(userAuths);
    }
    public UserAuths getUserAuthByUsername(@NotNull String username){
        return userAuthsRepository.findByUsername(username);

    }
    public List<UserAuths> getAll(){
        return userAuthsRepository.findAll();
    }
    public UserAuths getByUser(Users users){
        return userAuthsRepository.findByUser(users);
    }
}
